package Controller;

import java.io.PrintWriter;
import java.util.Date;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceResponse;
import javax.servlet.ServletException;

import models.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.ModelAndView;

import Service.DateFormatter;
import Service.UserService;
import cn.vlabs.vwb.LoginContext;

@Controller
@RequestMapping(value = "VIEW")
public class LoginController extends BaseController {

	@Autowired
	private UserService userService;
	
	@RequestMapping(params = "action=loginErrorView")
	public ModelAndView loginErrorView(RenderRequest request,
			RenderResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.addObject("isLoginFailed", "true");
		mv.setViewName("register");
		return mv;
	}

	@RequestMapping(params = "action=login")
	public void login(ActionRequest request, ActionResponse response)
			throws Exception {
		String email = request.getParameter("loginEmail");
		System.out.println("email"+email+":"+userService.userExists(email));
		String pass = request.getParameter("loginPass");
		userService.setCurrentUser(email);
		doLogin(request, response, email, pass);
	}
	
	@RequestMapping(params = "action=registeruser")
	public void register(ActionRequest request, ActionResponse response)
			throws Exception {
		String email = request.getParameter("email");
		String pass = request.getParameter("pass");
		
		
		//add user into umt
		userService.createUserInUMT(email, pass);
		userService.addUserToVO(email);
		
		//create new User
		String fname= request.getParameter("fname");
		String lname= request.getParameter("lname");
		String mname= request.getParameter("mname");
		String company= request.getParameter("unit");
		String jobtitle = request.getParameter("position");
		String cell = request.getParameter("contact");
		int userLevel = 0;
		User newuser = new User();
		newuser.setEmail(email);
		newuser.setFname(fname);
		newuser.setLname(lname);
		newuser.setMname(mname);
		newuser.setCompany(company);
		newuser.setJobtitle(jobtitle);
		newuser.setCellphone(cell);
		newuser.setUserLevel(userLevel);
		newuser.setRegDate(DateFormatter.format(new Date()));
		//add user into data base
		userService.addUser(newuser);
		userService.setCurrentUser(newuser);
		
		doLogin(request, response, email, pass);
	}

	private void doLogin(ActionRequest request, ActionResponse response,
			String email, String pass) throws ServletException {
		PortletSession session = request.getPortletSession();
		String targetURL = (String) session.getAttribute("targetURL");
		String failURL = (String) session.getAttribute("failURL");
		System.out.println("tt:"+targetURL);
		System.out.println("ff:"+failURL);
		

		LoginContext context = new LoginContext(targetURL, failURL);
		context.setUserName(email);
		context.setPassword(pass);
		response.doLogin(context);
	}
	
	// check if user name already exits.
	@RequestMapping(params = "action=validateDup")
	public void validateRegisterDup(@RequestParam("email")
	String userId, ResourceResponse response) throws Exception {
		String result = "false";
		if (null != userId && !"".equals(userId)) {
			boolean userExists = userService.userExists(userId);
			if (!userExists) {
				result = "true";
			}
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = response.getWriter();
		writer.print(result);
	}
	
	
	
	
	

}

